# -*- coding: windows-1252 -*-

import sys
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
import Skype4Py
import SocketServer
import os
import subprocess as subp

# only linux
""" 
import win32gui
import win32con

# ###################################################
# ###################################################
class WindowController :
	@staticmethod
	def getProcessByTitle(title ):
		hwnd = win32gui.FindWindowEx(0,0,0, title)
		return hwnd
	
	@staticmethod
	def showXBMC():
		hwnd = WindowController.getProcessByTitle("XBMC Media Center")
		win32gui.ShowWindow(hwnd,win32con.SW_SHOWDEFAULT)
		win32gui.SetForegroundWindow(hwnd)
	
	@staticmethod
	def hideXBMC():
		hwnd = WindowController.getProcessByTitle("XBMC Media Center")
		win32gui.ShowWindow(hwnd,win32con.SW_HIDE)
		
	@staticmethod
	def showSkype():
		hwnd = win32gui.FindWindow('tSkMainForm.UnicodeClass',None)
		#win32gui.ShowWindow(hwnd,win32con.SW_SHOWMAXIMIZED)
		#win32gui.ShowWindow(hwnd,win32con.SW_MAX)
"""
# ###################################################
# ###################################################
class ConnectorService :

	def __init__(self) :
		self.notifyUrl = "http://localhost:9991"
		pass
	
	# start skype if not start yet, then attach 
	def startSkype(self):
		self.skype = Skype4Py.Skype()
		if not self.skype.Client.IsRunning :
			self.skype.Client.Start()
		
		self.skype.Attach()
		#self.skype.SilentMode = True
		self.skype.Client.Minimize()
		self.skype.OnCallStatus = self.onCall
	
	# on call event handler
	def onCall(self,call, status): 	
		currentUser = self.skype.CurrentUser.Handle
		desc = call.PartnerHandle
		
		user = self.skype.CurrentUser.DisplayName
		
		if currentUser != desc :
			if status == "INPROGRESS" :
				
				try :
					call.startVideoSend()
					call.startVideoReceive()
				except :
					pass
				
				#WindowController.hideXBMC()
				#WindowController.showSkype()
				os.system("wmctrl -s 1")

				p = subp.Popen("""wmctrl  -lp | grep "| Call with" | awk '{print $1}'""", shell=True, stdout=subp.PIPE)
				p.wait()
				win_id = p.stdout.readline().strip()
				os.system("wmctrl -i -r %s -t 1" % (win_id))
				
			elif status == 'FINISHED' :
				#WindowController.showXBMC()
				#WindowController.hideSkype()
				#self.skype.SilentMode = True
				os.system("wmctrl -s 0")
				
		print 'status ', status
		print 'desc', desc
		print "user", user
		print "type", call.Type
	
	# hang up all
	def hangUp(self):
		calls = self.skype.ActiveCalls
		for call in calls :
			call.Finish()
		return 0
	
	# start call specific skype user
	def call(self,skypeName):
		try : self.hangUp()
		except : pass
		self.skype.PlaceCall(skypeName)
		
		return  0
	
	# get friend list
	def getAllFriend(self):
		contact = []
		friends = self.skype.Friends
		for friend in friends :
			user = {}
			user['SKYPE_NAME'] = friend.Handle
			user['STATUS'] = friend.OnlineStatus
			contact.append(user)
		return contact

	# start skype and xmlrpc service --
	def start(self):
		self.startSkype()

		server = SimpleXMLRPCServer(('localhost', 8001))
		server.register_function(self.call)
		server.register_function(self.getAllFriend)
		server.register_function(self.hangUp)
		#server.register_function(self.getCallStatus)
		
		print 'start service @ localhost 8001'
		server.serve_forever()
		
		
if __name__ == '__main__' :
	c = ConnectorService()
	c.start()
